package org.dolphin.entity.measurement;

import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.ForeignKey;
import org.springframework.util.ObjectUtils;

@Embeddable
public class MeasurementConversionPk implements Serializable {
  private static final long serialVersionUID = 6606398995103594798L;
  private UnitOfMeasurement source;
  private UnitOfMeasurement target;

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof MeasurementConversionPk) {
      MeasurementConversionPk oth = (MeasurementConversionPk) obj;
      return ObjectUtils.nullSafeEquals(getSource(), oth.getSource())
          && ObjectUtils.nullSafeEquals(getTarget(), oth.getTarget());
    }
    return super.equals(obj);
  }

  @ManyToOne(optional = false)
  @ForeignKey(name = "FK_XS_MEASUREMENT_CONVERSION_SOURCE")
  public UnitOfMeasurement getSource() {
    return source;
  }

  @ManyToOne(optional = false)
  @ForeignKey(name = "FK_XS_MEASUREMENT_CONVERSION_TARGET")
  public UnitOfMeasurement getTarget() {
    return target;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getSource() != null ? getSource().getId() : 0L);
    hash ^= ObjectUtils.nullSafeHashCode(getTarget() != null ? getTarget().getId() : 0L);
    return hash;
  }

  public void setSource(UnitOfMeasurement source) {
    this.source = source;
  }

  public void setTarget(UnitOfMeasurement target) {
    this.target = target;
  }
}
